package edu.chalmers.AV_2DPlatformerFramework.Model;

import edu.chalmers.AV_2DPlatformerFramework.Utility.Vector2D;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Represents all the objects in the current map.
 * @version 0.3
 * @author Andrea, Richard
 * @revised Andrea, Richard: 08-05-14
 * @revised Richard: Added GameWorld handling.
 * @revised Richard: Refactoring to use a single list of GameObjects. Originals are now placed in the world.
 *                      Scapped activeObjects due to concurrency issues.
 * @revised Richard: Added main actor. Used by model to chase. Implemented Strata sorting. Cleaning. Removed unused variables. Removed ActiveObjects concept.
 */
public class GameMap {
    
    private final ArrayList<GameObject> objects = new ArrayList();
    private static final StrataComparator sc = new StrataComparator();

    private Vector2D gravity = new Vector2D(0, 0.08);
    private GameObject mainActor;
    
    /**
     * Creates a new GameMap.
     * @param objects The objects to place in this GameMap, based on their Position.
     * @param world The world.
     */
    public GameMap(GameObject[] objects, GameWorld world){   
        this.objects.add(world);
        Arrays.sort(objects, sc);
        this.objects.addAll(Arrays.asList(objects));
    }
    
    /**
     * Changes the gravity vector for this GameMap. Default 0.02 pixels/cycle downwards.
     * @param newGravity The new gravity vector.
     */
    public void setGravity(Vector2D newGravity){
        gravity = newGravity;
    }
    /**
     * Fetches the gravity used by this GameMap. Default 0.02 pixels/cycle downwards.
     * @return The gravity vector used by this GameMap.
     */
    public Vector2D getGravity(){
        return gravity;
    }

        /**
     * Returns an ArrayList with all objects.
     * @return returns some stuff.
     */
    public synchronized ArrayList<GameObject> getObjects(){ 
          return objects;
    } 
    
    /**
     * Set the mainActor for this map, typically the player. Can be any object of interest at the time.
     * @param newMainActor The new main actor.
     */
    public void setMainActor(GameObject newMainActor){
        this.mainActor = newMainActor;
    }
    /**
     * Returns the mainActor for this map, typically the player. Can be any object of interest at the time.
     * @return The current main actor.
     */
    public GameObject getMainActor(){
        return mainActor;
    }
}